import React from 'react';
import { connect } from 'dva';
import { createForm } from 'rc-form';
import styles from './index.less';
import {Toast,Switch,List,InputItem,Button,Modal,WingBlank,WhiteSpace} from 'antd-mobile';
import {routerRedux} from 'dva/router';
import Block from 'fs-flex'
import Header from '../../../../../components/Header/index'
import WindowService from '../../../../../services/windowService'
const alert = Modal.alert
class SettingPage extends React.Component{
  state={
      show:false,
  }
    componentDidMount(){
        //console.log(this.props.location.state.data)
    }
    async save(data){
        console.log(data)
        if(!data.password){
            delete data.password
        }
        console.log(data)
        if(data.id){
            const {code,msg} = await WindowService.updateWindow(data)
            if(code==="0000"){
                Toast.info("更新成功！",2)
                setTimeout(() => {
                    this.props.history.goBack()
                }, 2000);
            }else{
                Toast.info(msg,3)
            }
        }else{
            const { code,msg }= await WindowService.addWindow(data)
            if(code==="0000"){
                Toast.info("添加成功！",3)
                setTimeout(() => {
                    this.props.history.goBack()
                }, 2000);
            }else{
                Toast.info(msg,3)
            }
        }
    }
  
  render(){
      const { getFieldProps  } = this.props.form;
      const originData = this.props.location.state?this.props.location.state.data:null;
    return (
      <Block vf>
        <Block>
          <Header title='窗口信息编辑' needBack='true' history={this.props.history}/>
        </Block>
        <WingBlank size="lg">
            <WhiteSpace size="lg" />
                <List>
                    <List.Item>
                        <InputItem
                            {...getFieldProps('windowName',{initialValue:originData?originData.windowName:""})}
                            clear
                            placeholder="请输入窗口名称"
                            ref={el => this.autoFocusInst = el}
                        >窗口名称:</InputItem>
                    </List.Item>
                    <List.Item>
                        <InputItem
                            {...getFieldProps('account',{initialValue:originData?originData.account:""})}
                            clear
                            placeholder="请输入账号"
                            ref={el => this.autoFocusInst = el}
                        >账号：</InputItem>
                    </List.Item>
                    <List.Item>
                        <InputItem
                            {...getFieldProps('password')}
                            clear
                            placeholder="请输入密码"
                            ref={el => this.autoFocusInst = el}
                        >密码：</InputItem>
                    </List.Item>
                    <List.Item>窗口服务状态：
                        <Switch
                            {...getFieldProps('status', {
                                initialValue: originData?originData.status:true,
                                valuePropName: 'checked',
                            })}
                            onClick={(checked) => { console.log(checked);  }}
                        />
                    </List.Item>
                </List>
            <WhiteSpace size="lg" />
            <Block wf mt={15}>
                <Block f='3'></Block>
                <Block f='4'><Button a='c' f='c' size='small' type="primary" onClick={() =>
                    {
                        let data = this.props.form.getFieldsValue()
                        data['id']=originData?originData.id:null
                        this.save(data)
                    }}
                    >保存</Button></Block>
                <Block f='3'></Block>
            </Block>
        </WingBlank>
      </Block>
    );
  }
  
}

SettingPage
.propTypes = {
};

export default createForm()(connect()(SettingPage));
